// 77e9ae669b8b59d2fbf4a9e8f23eb5fb14df17bf448b98504d6d9f23e152c4f7

function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = decodeURI(window.location.search.substr(1)).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

function getSessionKey(openId, axios) {
  return new Promise(function(resolve, reject) {
    const params = { openId: openId };
    axios.get("/login/pcLogin", { params }).then((res) => {
      if (res.success) {
        sessionStorage.setItem("SESSIONKEY", res.sessionKey);
        sessionStorage.setItem("SESSIONINFO", res.perfectInformation);
        axios.interceptors.request.use(
          (config) => {
            var SESSIONKEY = sessionStorage.getItem("SESSIONKEY");
            if (SESSIONKEY) {
              config.headers.SESSIONKEY = SESSIONKEY;
            }
            return config;
          },
          (error) => {
            return Promise.reject(error);
          }
        );
        resolve();
      } else {
        reject(res.msg);
      }
    });
  });
}

function oldLoginAuth(axios) {
  //暂时性清数据
  if (!localStorage.getItem("clearTimes")) {
    localStorage.clear();
    sessionStorage.clear();
    localStorage.setItem("clearTimes", 1);
  }

  const openId = getQueryString("openId");
  console.log("openid为：" + openId);
  const dateNow = new Date().getTime();
  if (openId) {
    location.search = "";
    localStorage.setItem("openId", openId);
    localStorage.setItem("loginTime", dateNow);
    return getSessionKey(openId, axios);
  } else {
    const lastLogin = localStorage.getItem("loginTime")
      ? parseInt(localStorage.getItem("loginTime"))
      : null;
    const diffHours = lastLogin
      ? parseInt(
          ((dateNow - lastLogin) % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
        )
      : null;

    const localOpenId = localStorage.getItem("openId");
    console.log("相差小时：" + diffHours);
    if (diffHours >= 4 || (!diffHours && diffHours != 0)) {
      localStorage.setItem("openId", "");
      sessionStorage.removeItem("SESSIONKEY");
      sessionStorage.removeItem("SESSIONINFO");
      location.href =
        "https://minitool.xuexiwangzhe.com/miniapp/wechat/qrAuthorize?returnUrl=https://minitool.xuexiwangzhe.com/minitool/index.html";
    } else {
      const sessionKey = sessionStorage.getItem("SESSIONKEY");
      if (!sessionKey) {
        return getSessionKey(localOpenId, axios);
      }
    }
  }
}

module.exports.loginAuth = function() {
  const dateNow = new Date().getTime();

  const lastLogin = localStorage.getItem("loginTime")
    ? parseInt(localStorage.getItem("loginTime"))
    : null;
  const diffHours = lastLogin
    ? parseInt(
        ((dateNow - lastLogin) % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
      )
    : null;

  console.log("相差小时：" + diffHours);
  if (diffHours >= 4 || (!diffHours && diffHours != 0)) {
    sessionStorage.removeItem("SESSIONKEY");
    location.href = "newLogin.html";
  } else {
    const sessionKey = sessionStorage.getItem("SESSIONKEY");
    if (!sessionKey) {
      location.href = "newLogin.html";
    }
  }
};

module.exports.getQiniuTokens = function(axios) {
  return new Promise((resolve, reject) => {
    axios
      .get("https://minitool.xuexiwangzhe.com/miniapp/qiniu/token")
      .then((res) => {
        if (!res) console.log("token获取出错，token值为：" + res);
        resolve(res);
      });
  });
};
